<?php
class Db_Base{
	
	public static $instance = NULL;
	public $connect = NULL;
	protected $dbName = 'test';
	protected $host = '127.0.0.1';
	protected $port = 3306;
    protected $_dbUser = 'root';
    protected $_dbPwd = '198754';
   	public $error = array();
   	public $result = array();
   	private $affectedCount = NULL;
   	public $isDebug = FALSE;

    private function __construct(){
        $this->init();
    }

    protected function init(){
        $host = $this->host;
        $port = $this->port;
        $dbName = $this->dbName;
    	try {
    	    $dsn = "mysql:host={$host};port={$port};dbname={$dbName}";
			$pdo = new PDO($dsn, $this->_dbUser, $this->_dbPwd, array(
						PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE,
						PDO::ATTR_EMULATE_PREPARES => TRUE,
						PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
						PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
						PDO::ATTR_TIMEOUT => 10,
					)
				);
		} catch (PDOException $e) {
			exit('DB error:'.$e->getMessage());
			$pdo = NULL;
		}

		$this->connect = $pdo;
    }

    public function setDebug(){
    	$this->isDebug = TRUE;
    }

    private function __clone(){}

    public static function getInstance(){
    	if(self::$instance === NULL){
    		self::$instance = new Db_Base();
    	}
    	return self::$instance;
	}

	public function select($sql){

		if(!$this->query($sql)){
			return FALSE;
		}

		return $this->result->fetchAll();
	}

	public function selectOne($sql){

		if(!$this->query($sql)){
			return FALSE;
		}

		return $this->result->fetch();
	}

	public function selectCount($sql){

		if(!$this->query($sql)){
			return FALSE;
		}

		$one = $this->result->fetch();
		if(isset($one['result'])){
			return $one['result'];
		}

		return FALSE;

	}

	public function update($sql){
		if(!$this->execute($sql)){
			return FALSE;
		}

		return TRUE;
	}

	public function insert($sql){
		if(!$this->execute($sql)){
			return FALSE;
		}

		return TRUE;
	}

	public function delete($sql){
		if(!$this->execute($sql)){
			return FALSE;
		}

		return TRUE;
	}

	public function getAffectedCount(){
		return $this->affectedCount;
	}

	public function query($sql){
		//加异常判断
		$flag = TRUE;
	    try{
			$con = $this->connect;
			$rs = $con->query($sql);
			if(!$rs){
				$flag = FALSE;
				$rs = NULL;
				$this->error[] = exit('DB error:'.$con->errorInfo());
			}
		} catch (PDOException $e) {
			$this->error[] = exit('DB error:'.$e->getMessage());
			$flag = FALSE;
			$rs = NULL;
		}

		$this->result = $rs;

		if($this->isDebug){
			echo "LOOK RESULT :";
			echo "<br>\n-----------------------------------<br>\n";
			echo "<pre>";
			var_dump($this->result);
			echo "<pre>";
			echo "<br>\n-----------------------------------<br>\n";
		}

		return $flag;
	}

	public function execute($sql){
		//加异常判断
		$flag = TRUE;
	    try{
			$con = $this->connect;
			$rs = $con->exec($sql);
			if(!$rs){
				$flag = FALSE;
				$rs = NULL;
				$this->error[] = exit('DB error:'.$con->errorInfo());
			}
		} catch (PDOException $e) {
			$this->error[] = exit('DB error:'.$e->getMessage());
			$flag = FALSE;
			$rs = NULL;
		}

		$this->affectedCount = $rs;

		if($this->isDebug){
			echo "LOOK  affected number:";
			echo "<br>\n-----------------------------------<br>\n";
			echo "<pre>";
			var_dump($this->affectedCount);
			echo "<pre>";
			echo "<br>\n-----------------------------------<br>\n";
		}

		return $flag;
	}

}